package com.hoshiicloud.suppplier.controller.companyorganization.goodsmanage;

import com.hoshiicloud.auth.vo.TokenUsersVO;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.token.UserInfoUtil;
import com.hoshiicloud.suppplier.param.SupplierSpuDetailParam;
import com.hoshiicloud.suppplier.param.TenantSupplierGoodsParam;
import com.hoshiicloud.suppplier.service.companyorganization.GoodsReleaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  商品发布 和 商品编辑
 * @author linjx on 2019/8/12
 */
@RestController
@RequestMapping("/goods/goods-release-edit")
@Api(value = "商品管理-我要供货/商品列表", tags = {"商品管理-我要供货/商品列表"})
public class GoodsReleaseController {

    @Autowired
    private GoodsReleaseService goodsReleaseService;


    @ApiOperation(value = "根据类目id,查询商品发布基础信息")
    @GetMapping("/query")
    public Response queryMessage(@RequestParam("categoryId") Long categoryId, HttpServletRequest request) {
        Map<String, Object> myMap = new HashMap<>();
        TokenUsersVO user = UserInfoUtil.getUserInfo(request);
        myMap.put("companyId", user.getCompanyId());
        myMap.put("platformId", user.getPlatId());
        myMap.put("storeId", user.getStoreId());
        return Response.data(goodsReleaseService.queryBaseMessage(categoryId, myMap));
    }


    @ApiOperation(value = "租户供应商品发布商品")
    @PostMapping("/releaseGoods")
    public Response releaseGoods(@RequestBody SupplierSpuDetailParam spuDetailParam, HttpServletRequest rq) {
        TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        spuDetailParam.setCompanyId(user.getCompanyId());
        spuDetailParam.setPlatformId(user.getPlatId());
        spuDetailParam.setStoreId(user.getStoreId());
        spuDetailParam.setCreateBy(user.getRealName());
        return Response.data(goodsReleaseService.releaseGoods(spuDetailParam));
    }

    @ApiOperation(value = "分页查询供应商品发布的商品信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "第几页", type = "Long"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", type = "Long"),
            @ApiImplicitParam(name = "categoryId", value = "第三级类目Id", type = "Integer"),
            @ApiImplicitParam(name = "purchaseType", value = "采购类型", type = "Integer"),
            @ApiImplicitParam(name = "skuNo", value = "商品编码", type = "String"),
            @ApiImplicitParam(name = "skuTitle", value = "商品名称", type = "String")
    })
    @PostMapping("/listOfGoods")
    public Response listOfGoods(@RequestBody TenantSupplierGoodsParam supplierGoodsParam, HttpServletRequest request) {
        TokenUsersVO userInfo = UserInfoUtil.getUserInfo(request);
        supplierGoodsParam.setCompanyId(userInfo.getCompanyId());
        supplierGoodsParam.setPlatformId(userInfo.getPlatId());
        supplierGoodsParam.setStoreId(userInfo.getStoreId());
        return Response.data(goodsReleaseService.listOfGoods(supplierGoodsParam));
    }

    @ApiOperation(value = "查询需要编辑的商品的信息")
    @PostMapping("/getNeedEditDate")
    public Response getNeedEditDate(@RequestBody TenantSupplierGoodsParam supplierGoodsParam, HttpServletRequest request) {
        TokenUsersVO user = UserInfoUtil.getUserInfo(request);
        supplierGoodsParam.setCompanyId(user.getCompanyId());
        supplierGoodsParam.setPlatformId(user.getPlatId());
        supplierGoodsParam.setStoreId(user.getStoreId());
        return Response.data(goodsReleaseService.getNeedEditDate(supplierGoodsParam));
    }

    @ApiOperation(value = "商品编辑保存")
    @PutMapping("/editForGoods")
    public Response editForGoods(@RequestBody TenantSupplierGoodsParam supplierGoodsParam) {
        return Response.data(goodsReleaseService.editForGoods(supplierGoodsParam));
    }

    @ApiOperation(value = "删除商品信息")
    @DeleteMapping("/deleteReleaseGoods")
    public Response deleteReleaseGoods(@RequestBody TenantSupplierGoodsParam supplierGoodsParam) {
        return Response.data(goodsReleaseService.deleteReleaseGoods(supplierGoodsParam));
    }

    @ApiOperation(value = "批量删除商品信息")
    @DeleteMapping("/deleteBatchGoods")
    public Response deleteBatchGoods(@RequestBody List<TenantSupplierGoodsParam> goodsParams) {
        return Response.data(goodsReleaseService.deleteBatchGoods(goodsParams));
    }

    @ApiOperation(value = "分页查询需要报价的商品的BaseSpu信息")
    @PostMapping("/listOfferGoods")
    public Response listOfferGoods(@RequestBody TenantSupplierGoodsParam goodsParam, HttpServletRequest request) {
        TokenUsersVO userInfo = UserInfoUtil.getUserInfo(request);
        goodsParam.setCompanyId(userInfo.getCompanyId());
        goodsParam.setPlatformId(userInfo.getPlatId());
        goodsParam.setStoreId(userInfo.getStoreId());
        return Response.data(goodsReleaseService.ListBaseSpu(goodsParam));
    }

    @ApiOperation(value = "获得需要编辑报价的商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "baseSpuId", value = "baseSpu的Id", type = "Long"),
            @ApiImplicitParam(name = "vendorId", value = "供应商Id需要前端获取传回来", type = "Long")
    })
    @PostMapping("/getNeedEditOfferGoods")
    public Response getNeedEditOfferGoods(TenantSupplierGoodsParam goodsParam, HttpServletRequest request) {
        TokenUsersVO user = UserInfoUtil.getUserInfo(request);
        goodsParam.setCompanyId(user.getCompanyId());
        goodsParam.setPlatformId(user.getPlatId());
        goodsParam.setStoreId(user.getStoreId());
        return Response.data(goodsReleaseService.getNeedEditOfferGoods(goodsParam));
    }

    /**
     * TODO 供应商Id需要前端传过来
     */
    @ApiOperation(value = "编辑需要报价的商品")
    @PostMapping("/editOfferGoods")
    public Response editOfferGoods(@RequestBody SupplierSpuDetailParam spuDetailParam) {
        return Response.data(goodsReleaseService.editOfferGoods(spuDetailParam));
    }
}
